package com.liehu.business.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liehu.business.dao.ArticleMapper;
import com.liehu.business.pojo.Article;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
@Transactional //事务
public class ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private RedisTemplate redisTemplate;

    private  HttpServletRequest request;

    /**
     * 查询所有
     */
    public List<Article> findAll(){
        return articleMapper.selectList(null);
    }
    /**
     * 根据id查询
     */
    public Article findById(String id){
        Article article = articleMapper.selectById(id);
        article.setViews(article.getViews()+1);//浏览数+1
        articleMapper.updateById(article);
        return article;
    }
    /**
     * 分页查询
     */
    public Page<Article> findByPage(Integer page, Integer size){
        Page<Article> pageable = new Page<>(page,size);//创建分页对象
        return  articleMapper.selectPage(pageable, null);
    }
    /**
     * 条件分页查询
     */
    public Page<Article> findByPage(Article article,Integer page,Integer size){
        Page<Article> pageable = new Page<>(page,size);//创建分页对象
        QueryWrapper<Article> wrapper = myWrapper(article);//创建条件对象wrapper
        return  articleMapper.selectPage(pageable, wrapper);

    }
    /**
     * 内部处理条件
     * @param article
     * @return
     */
    private QueryWrapper<Article> myWrapper(Article article){
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        //条件构造1
        if(article==null){
            wrapper.eq("published","1")
                    .eq("recommend","1")
                    .eq("status","1");
        }else {
            if(article.getType_id()!=null){
                wrapper.eq("type_id",article.getType_id());
            }
            if(article.getPublished()!=null){
                wrapper.eq("published",article.getPublished());
            }
            if(article.getStatus()!=null){
                wrapper.eq("status",article.getStatus());
            }
            if(article.getRecommend()!=null){
                wrapper.eq("recommend",article.getRecommend());
            }
        }
        return  wrapper;
    }
    /**
     * 插入/保存
     */
    public void save(Article article){
        Claims claims_admin = (Claims) request.getAttribute("claims_admin");
        if(isAdmin(claims_admin)){
            article.setId(idWorker.nextId()+"");
            article.setPublished("0");
            articleMapper.insert(article);
        }

    }
    /**
     * 插入/发布
     */
    public void publish(Article article){
        Claims claims_admin = (Claims) request.getAttribute("claims_admin");
        if(isAdmin(claims_admin)){
            //给这条记录插入id
            article.setId(idWorker.nextId()+"");
            article.setPublished("1");
            articleMapper.insert(article);
        }

    }
    /**
     * 更新
     */
    public void update(Article article){
        Claims claims_admin = (Claims) request.getAttribute("claims_admin");
        if(isAdmin(claims_admin)){
            articleMapper.updateById(article);
        }
    }
    /**
     * 根据id删除
     */
    public void deleteById(String id){
        Claims claims_admin = (Claims) request.getAttribute("claims_admin");
        if(isAdmin(claims_admin)){
            articleMapper.deleteById(id);
        }
    }
    /**
     * 根据id点赞
     */
    public boolean thumbupById(String id){
        boolean flag =false;
        Article article = new Article();
        article.setId(id);
        Claims claims_user = (Claims) request.getAttribute("claims_user");
        String userId = claims_user.getId();//用户id
        if(claims_user==null){//游客或者管理员
            article.setThumbup(article.getThumbup()+1);//点赞数+1
            flag = true;
        }else {
            String  thumbup= (String)redisTemplate.opsForValue().get("thumbup" + userId);
            if(thumbup==null){
                redisTemplate.opsForValue().set("thumbup"+userId,"点赞成功");
                article.setThumbup(article.getThumbup()+1);//点赞数+1
                articleMapper.updateById(article);
                flag =true;
            }else{
                redisTemplate.delete("thumbup"+userId);
                article.setThumbup(article.getThumbup()-1);//点赞数-1
                articleMapper.updateById(article);
            }
        }
        return flag;

    }
    public boolean isAdmin(Claims claims_admin){
        if(claims_admin==null){
            new RuntimeException("权限不足");
            return false;
        }else{
            return true;
        }
    }


}
